package zw_901_1000.zw_929_独特的电子邮件地址;

import java.util.HashSet;
import java.util.Set;

class Solution {

    public static void main(String[] args) {
        String[] emails = {"test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"};
        int emails1 = numUniqueEmails(emails);
        System.out.println(emails1);

    }

    public static int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet<>();
        for (String s : emails) {
            StringBuilder sb = new StringBuilder();
            int n = s.length(), i = 0;
            boolean ok = true;
            while (i < n) {
                char c = s.charAt(i);
                if (c == '@') break;
                if (c == '.' && ++i >= 0) continue;
                if (c == '+') ok = false;
                if (ok) sb.append(c);
                i++;
            }
            set.add(sb.append(s.substring(i)).toString());
        }
        return set.size();
    }



//    public static int numUniqueEmails(String[] emails) {
//        Set<String> emailSet = new HashSet<String>();
//        for (String email : emails) {
//            int i = email.indexOf('@');
//            String local = email.substring(0, i).split("\\+")[0]; // 去掉本地名第一个加号之后的部分
//            local = local.replace(".", ""); // 去掉本地名中所有的句点
//            emailSet.add(local + email.substring(i));
//        }
//        return emailSet.size();
//    }

}
